package cn.dankal.flyaudio.presenter;

import com.jeremyliao.liveeventbus.LiveEventBus;

import java.util.HashMap;
import java.util.Map;

import androidx.annotation.NonNull;

import api.ShopUserServiceFactory;
import cn.dankal.basiclib.pojo.BaseListResponse;
import cn.dankal.basiclib.pojo.BaseResponse;
import cn.dankal.basiclib.pojo.CommentsEntity;
import cn.dankal.basiclib.pojo.ProductEntity;
import cn.dankal.basiclib.pojo.req.AddShopCarReq;
import cn.dankal.basiclib.pojo.req.FavoritesShopReq;
import cn.dankal.basiclib.rx.AbstractSubscriber;
import cn.dankal.basiclib.util.ToastUtils;
import cn.dankal.flyaudio.contract.ProductDetailContract;

public class ProductDetailPresenter implements ProductDetailContract.Presenter {

    private ProductDetailContract.View mView;

    @Override
    public void attachView(@NonNull ProductDetailContract.View view) {
        mView = view;
    }

    @Override
    public void detachView() {
        mView = null;
    }

    @Override
    public void fetchProductDetail(String uuid) {
        mView.showLoadingDialog();
        ShopUserServiceFactory.shopDetail(uuid)
                .subscribe(new AbstractSubscriber<BaseResponse<ProductEntity>>(mView) {
                    @Override
                    public void onNext(BaseResponse<ProductEntity> productEntityBaseResponse) {
                        mView.dismissLoadingDialog();
                        mView.handleProductDetailResult(productEntityBaseResponse.getData());
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        mView.dismissLoadingDialog();
                    }
                });
    }

    @Override
    public void favoritesProduct(String productUuid) {
        FavoritesShopReq req = new FavoritesShopReq();
        req.setProductUuid(productUuid);
        mView.showLoadingDialog();
        ShopUserServiceFactory.favoritesShop(req)
                .subscribe(new AbstractSubscriber<BaseResponse<Object>>(mView) {
                    @Override
                    public void onNext(BaseResponse<Object> productEntityBaseResponse) {
                        mView.dismissLoadingDialog();
                        ToastUtils.showShort("收藏成功");
                        mView.favoritesProductSuccess();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        mView.dismissLoadingDialog();
                    }
                });
    }

    @Override
    public void cancelFavoritesProduct(String productUuid) {
        mView.showLoadingDialog();
        ShopUserServiceFactory.cancelFavoritesShop(productUuid)
                .subscribe(new AbstractSubscriber<BaseResponse<Object>>(mView) {
                    @Override
                    public void onNext(BaseResponse<Object> productEntityBaseResponse) {
                        mView.dismissLoadingDialog();
                        ToastUtils.showShort("取消收藏");
                        mView.cancelFavoritesProductSuccess();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        mView.dismissLoadingDialog();
                    }
                });
    }

    @Override
    public void addShopCar(String productUuid, int count) {
        mView.showLoadingDialog();
        AddShopCarReq req = new AddShopCarReq();
        if (!"-1".equals(productUuid)) {
            req.setItemUuid(productUuid);
        }
        req.setItemCount(count + "");
        ShopUserServiceFactory.addShopCar(req)
                .subscribe(new AbstractSubscriber<BaseResponse<Object>>(mView) {
                    @Override
                    public void onNext(BaseResponse<Object> productEntityBaseResponse) {
                        mView.dismissLoadingDialog();
                        LiveEventBus.get("ShopCarNum",String.class).post("1");
                        ToastUtils.showShort("加入购物车成功");
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        mView.dismissLoadingDialog();
                    }
                });
    }

    @Override
    public void getEvaluate(String uuid, int page) {
        mView.showLoadingDialog();
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("productUuid", uuid);
        queryMap.put("page", page);
        queryMap.put("size", "10");
        ShopUserServiceFactory.comments(queryMap)
                .subscribe(new AbstractSubscriber<BaseResponse<BaseListResponse<CommentsEntity>>>(mView) {
                    @Override
                    public void onNext(BaseResponse<BaseListResponse<CommentsEntity>> baseListResponseBaseResponse) {
                        mView.dismissLoadingDialog();
                        mView.evaluateSuccess(baseListResponseBaseResponse.getData().getList());
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        mView.dismissLoadingDialog();
                    }
                });
    }
    @Override
    public void like(String uuid) {
        mView.showLoadingDialog();
        ShopUserServiceFactory.likeCommentsProduct(uuid)
                .subscribe(new AbstractSubscriber<BaseResponse<Object>>(mView) {
                    @Override
                    public void onNext(BaseResponse<Object> objectBaseResponse) {
                        mView.dismissLoadingDialog();
                        mView.likeSuccess();
                    }
                });
    }

    @Override
    public void unLike(String uuid) {
        mView.showLoadingDialog();
        ShopUserServiceFactory.unLikeCommentsProduct(uuid)
                .subscribe(new AbstractSubscriber<BaseResponse<Object>>(mView) {
                    @Override
                    public void onNext(BaseResponse<Object> objectBaseResponse) {
                        mView.dismissLoadingDialog();
                        mView.unLikeSuccess();
                    }
                });
    }
}
